package cn.zhizihua.controller;


import cn.zhizihua.domain.po.Cart;
import cn.zhizihua.domain.po.Food;
import cn.zhizihua.domain.query.CartQuery;
import cn.zhizihua.domain.query.FoodQuery;
import cn.zhizihua.domain.vo.ResultBean;
import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.service.ICartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhizihua
 * @since 2024-12-11
 */

@Api(tags = "购物车项管理接口")
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    ICartService cartService;

    //cart（cartId(判断条件),foodId,businessId,userId,quantity）
    // 1、添加或修改（是否有cartid）
    @ApiOperation("购物车项添加或修改")
    @PostMapping
    public ResultBean save(@RequestBody Cart cart){
        System.out.println("----CartController-----save--------");
        System.out.println(cart);
        boolean result = cartService.cartSaveOrUpdate(cart);
        if (!result){
            throw new AppException(AppExceptionCodeMsg.SAVE_OR_UPDATE_FAILE);
        }
        return ResultBean.SUCCESS();
    }

    //  2、id删除
    @ApiOperation("根据购物车项id删除购物车信息")
    @DeleteMapping("/{id}")   // http://localhost:8080/cart/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------CartController--------del-------------");
        System.out.println(id);
        //删除购物车项
        boolean del = cartService.removeById(id);
        if (!del){
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    // 3、id查询
    @ApiOperation("根据购物车项id查询购物车信息")
    @GetMapping("/{id}")   // http://localhost:8080/cart/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------CartController--------getById-------------");
        System.out.println(id);
        //id查询购物车信息
        Cart byId = cartService.getById(id);
        System.out.println(byId);
        return ResultBean.SUCCESS(byId);
    }

    // 4、查寻全部购物车数据
    @ApiOperation("查询全部购物车信息")
    @GetMapping("/all")    //  get  /cart/all
    public ResultBean getAll(){
        System.out.println("-------CartController-----getAll--------");
        //获取全部数据
        List<Cart> list = cartService.list();
        if (list.size()==0){
            throw new AppException(AppExceptionCodeMsg.GET_ALL_FAILE);
        }
        return ResultBean.SUCCESS(list);
    }

    // 5、分页查询
    @ApiOperation("分页查询购物车信息")
    @PostMapping("/page")    //  post  /xxx/all
    public  ResultBean getByPage(@RequestBody CartQuery query){
        System.out.println("-------CartController-----page--------");
        System.out.println(query);
        Page<Cart> page = new Page<>(query.getPageNow(), query.getPageCount());
        cartService.page(page,query.getQueryWrapper());
        if (page.getTotal() == 0){
            throw new AppException(AppExceptionCodeMsg.GET_BY_PAGE_FAILE);
        }
        return ResultBean.SUCCESS(page);
    }
}
